<?php

namespace app\api\controller;

use app\common\model\ConvertGoods;
use app\common\model\ConvertOrder;
use app\common\model\FosterGoods;
use app\common\model\FosterOrder;
use app\common\model\Goods;
use app\common\model\JifenGoods;
use app\common\model\JifenOrder;
use app\common\model\Order;
use app\common\model\Plugin;
use app\common\model\StockGoods;
use app\common\model\StockOrder;
use app\common\model\UserWallet;
use app\common\service\PlatformBaseService;
use app\common\service\PlatFormFactory;
use app\common\util\MyException;
use Carbon\Carbon;
use hg\apidoc\annotation as Apidoc;
use Illuminate\Support\Collection;
use think\db\Builder;
use think\db\Query;
use think\facade\Db;

/**
 * @Apidoc\Title("订单")
 */
class OrderApi extends Base
{
    /**
     * @Apidoc\Title("保存订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/AddOrder")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("address_id",type="int",default="1",desc="地址id")
     * @Apidoc\Param("action",type="string",require=true,default="now",desc="购买方式/now立即购买|cart购物车结算|buy_again再次购买")
     * @Apidoc\Param("order_id",type="int",default="",desc="再次购买时必须")
     * @Apidoc\Param("pay_code",type="string",default="",desc="支付方式")
     * @Apidoc\Param("goods_id",type="int",default=1,desc="商品id/立即购买时必填")
     * @Apidoc\Param("goods_num",type="int",default="2",desc="购买商品数量/立即购买时必填")
     * @Apidoc\Param("act",type="string",require=true,default="yes",desc="是否生成订单")
     * @Apidoc\Param("user_note",type="string",default="",desc="用户备注")
     * @Apidoc\Param("paypwd",type="string",default="",desc="交易密码")
     */
    public function add_order(PlatFormFactory $platFormFactory)
    {
        //判断是否完善支付密码
        if (empty($this->user['paypwd'])) {
            throw new MyException('请先完善交易密码', -100);
        }
        $address_id = input('address_id');
        $order_id = input('order_id');
        if ($order_id) {
            $order_where['user_id'] = $this->user_id;
            $order_where['order_id'] = $order_id;
            $order = Order::where($order_where)->find();
        }
        if ($address_id) {
            $add = Db::name('user_address')->where('address_id', $address_id)->find();
        } else {
            if ($order_id) {
                if ($order) {
                    $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and address_id=' . $order['aid'])->find();
                    if (!$add) {
                        $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
                    }
                } else {
                    $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
                }
            } else {
                $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
            }
        }
        //收货地址
        $data['add'] = $add;
        $user = get_user_info($this->user_id);
        //购买方式：立即购买和购物车
        $action = input('action');
        $pay_code = input("pay_code");// 支付方式
        $goodsModel = new Goods();
        if ($action == 'now') {
            $goods_id = input("goods_id/d"); // 商品id
            $goods_num = input("goods_num/d");// 商品数量
            $item_id = input("item_id/d", 0); // 规格表id
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 100);
            }
            $temp_good = $goodsModel->where('goods_id=' . $goods_id . ' and is_on_sale=1')->find();
            if ($item_id) {
                $good = Db::name('spec_goods_price')->where('item_id', $item_id)->find();
                $goods_name = Db::name('goods')->where('goods_id=' . $goods_id . ' and is_on_sale=1')->value('goods_name');
                if (!$good['spec_img']) {
                    $good['spec_img'] = Db::name('goods')->where('goods_id=' . $goods_id . ' and is_on_sale=1')->value('goods_img');
                }
                $good_sn = Db::name('goods')->where('goods_id=' . $goods_id . ' and is_on_sale=1')->value('goods_sn');
                $goods_price = $good['price'];
                $store_count = $good['store_count'];
            } else {
                $good = $goodsModel->where('goods_id=' . $goods_id . ' and is_on_sale=1')->find();
                $goods_price = $good['price'];
                $store_count = $good['store_count'];
            }
            if (empty($good)) {
                throw new MyException('商品不存在或已下架', 100);
            }
            if ($goods_num > $store_count) {
                throw new MyException('商品库存不足', 100);
            }
            //设置商品订单表存储的数据
            $result['user_id'] = $this->user_id;
            $result['goods_id'] = $goods_id;
            $result['item_id'] = $item_id;
            $result['goods_name'] = $goods_name ?: $good['goods_name'];
            if ($good['key_name']) {
                $result['item_name'] = $good['key_name'];
            }
            $result['goods_img'] = $good['spec_img'] ? SITE_URL . $good['spec_img'] : SITE_URL . $good['goods_img'];
            $result['goods_sn'] = $good_sn ?: $good['goods_sn'];
            $result['goods_num'] = $goods_num;
            $result['goods_price'] = $goods_price;
            $result['store_count'] = $store_count;
            $cartList[] = $result;
            $data['goods'] = $cartList;
            $total_amount = $goods_price * $goods_num;
            $ship_price = $temp_good['ship_price'];
            $award = $temp_good['award'];
            $store_id = $temp_good['store_id'];
        } else {
            if ($action == 'buy_again') {
                if (empty($order)) {
                    throw new MyException("订单信息有误", 100);
                }
                $cartList = Db::name('order_goods')->where('order_id', $order_id)
                    ->field("goods_id,goods_num,item_id,item_name")
                    ->select()->toArray();
                if (empty($cartList)) {
                    throw new MyException("商品信息有误", 100);
                }
                foreach ($cartList as $key => $value) {
                    $good = $goodsModel->where('goods_id=' . $value['goods_id'] . ' and is_on_sale=1')->find();
                    $item = 1;
                    $max = $good['store_count'];
                    if ($value['item_id']) {
                        $item = Db::name('spec_goods_price')->where('item_id=' . $value['item_id'])->find();
                        $max = $item['store_count'];
                        $goods_price = $item['price'];
                    } else {
                        $goods_price = $good['price'];
                    }
                    if (empty($good) || empty($item)) {
                        $text = sprintf('%s商品不存在或已下架', $good['goods_name']);
                        throw new MyException($text, 100);
                    }
                    if (is_array($item)) {
                        if ($value['goods_num'] > $item['store_count']) {
                            $text = sprintf('%s商品已售尽', $good['goods_name']);
                            throw new MyException($text, 100);
                        }
                        $img = $item['spec_img'];
                    } else {
                        if ($value['goods_num'] > $good['store_count']) {
                            $text = sprintf('%s商品已售尽', $good['goods_name']);
                            throw new MyException($text, 100);
                        }
                    }
                    $value['store_count'] = $max;
                    $value['goods_price'] = $goods_price;
                    $value['goods_name'] = $good['goods_name'];
                    $value['goods_img'] = $img ? SITE_URL . $img : SITE_URL . $good['goods_img'];
                    //商品总价格
                    $total_amount += $value['goods_price'] * $value['goods_num'];
                    $cartList[$key] = $value;
                    $ship_price = $good['ship_price'];
                    $award = $good['award'];
                    $store_id = $good['store_id'];
                }
                $data['goods'] = $cartList;
            } else {
                throw new MyException('提交参数有误', 100);
            }
        }
        //商品实际付款价格
        $order_amount = $total_amount + $ship_price;

        $platformService = $platFormFactory->create();
        $payList = $this->getMethods($platformService);

        $data['total_amount'] = $total_amount;
        $data['xiaoji'] = $order_amount;
        $data['pay_list'] = $payList;
        $data['ship_price'] = $ship_price;
        //存储订单
        if (input('act') == 'yes') {
            //用户备注
            $user_note = input('user_note');
            $goods_num = input('goods_num/d');
            $openid = input('openid');
            //交易密码
            $paypwd = input('paypwd');
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 50);
            }
            if (empty($add)) {
                throw new MyException('请选择收货地址', 50);
            }

            $payMethod = $payList->where('code', $pay_code)->first();
            if (!$payMethod) {
                throw new MyException('支付方式有误', 50);
            }

            if ($goods_num > $cartList[0]['store_count']) {
                $msg = sprintf('此商品仅剩%s件', $cartList[0]['store_count']);
                throw new MyException($msg, 50);
            }
            $order_amount1 = $order_amount;
            switch ($pay_code) {
                case 'money':
                    if ($user['user_money'] < $order_amount1) {
                        $str = sprintf('粮票不足,可用%s!', $user['user_money']);
                        throw new MyException($str, 50);
                    }
                    if (empty($paypwd)) {
                        throw new MyException('请填写交易密码', 50);
                    }
                    if (encrypt($paypwd) != $this->user['paypwd']) {
                        throw new MyException('交易密码不正确', 50);
                    }
                    $pay['pay_name'] = '粮票';
                    $pay['pay_time'] = time();
                    $pay['order_status'] = 1;
                    break;
                case 'mcbt':
                    if ($this->user['mcbt'] < $order_amount1) {
                        $str = sprintf('牧场补贴不足,可用%s!', $this->user['mcbt']);
                        throw new MyException($str, 50);
                    }
                    if (empty($paypwd)) {
                        throw new MyException('请填写交易密码', 50);
                    }
                    if (encrypt($paypwd) != $this->user['paypwd']) {
                        throw new MyException('交易密码不正确', 50);
                    }
                    $pay['pay_name'] = '牧场补贴';
                    $pay['pay_time'] = time();
                    $pay['order_status'] = 1;
                    break;
                default:
                    $pay['pay_name'] = $payMethod['name'];
                    break;
            }
            $pay['pay_code'] = $pay_code;
            $pay['reduce'] = getSysConfig('shopping.reduce');
            $pay['order_sn'] = getOrderSn();//订单编号
            $pay['user_id'] = $this->user_id;//用户id
            $pay['store_id'] = $store_id;//商家id
            $pay['aid'] = $add['address_id'];//收货人
            $pay['consignee'] = $add['consignee'];//收货人
            $pay['mobile'] = $add['mobile'];//手机
            $pay['province'] = $add['province'];//省份
            $pay['city'] = $add['city'];//城市
            $pay['district'] = $add['district'];//县区
            $pay['address'] = $add['address'];//详细地址
            $pay['user_note'] = $user_note;
            $pay['total_amount'] = $total_amount;
            $pay['ship_price'] = $ship_price;
            $pay['order_amount'] = $order_amount1;
            $pay['award'] = $award;
            $order = new Order();
            $order->save($pay);
            $res = $order->order_id;
            if ($res) {
                foreach ($cartList as $v1) {
                    $goods2 = Db::name('goods')->where('goods_id', $v1['goods_id'])->find();
                    if ($v1['item_id']) {
                        $spec_img = Db::name('spec_goods_price')->where('item_id', $item_id)->value('spec_img');
                        $goods_img = $spec_img ?: $goods2['goods_img'];
                    } else {
                        $goods_img = $goods2['goods_img'];
                    }
                    $order_goods['order_id'] = $res;
                    $order_goods['user_id'] = $this->user_id;
                    $order_goods['goods_id'] = $v1['goods_id'];
                    $order_goods['goods_type'] = $goods2['goods_type'];
                    $order_goods['goods_name'] = $goods2['goods_name'];
                    $order_goods['goods_img'] = $goods_img;
                    $order_goods['item_id'] = $v1['item_id'];
                    $order_goods['item_name'] = $v1['item_name'];
                    $order_goods['goods_sn'] = $v1['goods_sn'];
                    $order_goods['goods_num'] = $v1['goods_num'];
                    $order_goods['goods_price'] = $v1['goods_price'];
                    $order_goods['cost_price'] = $goods2['cost_price'];
                    $order_goods['ship_price'] = $goods2['ship_price'];
                    $order_goods['award'] = $goods2['award'];
                    $order_goods['store_id'] = $goods2['store_id'];
                    if (in_array($pay['pay_code'], ['money', 'mcbt'])) {
                        $order_goods['is_zhifu'] = 1;
                        $order_goods['pay_time'] = time();
                    }
                    if ($v1['id']) {
                        //删除购物车
                        Db::name('cart')->where('id', $v1['id'])->delete();
                    }
                    //插入订单商品表
                    Db::name('order_goods')->insert($order_goods);
                }
                //如果余额支付直接扣除库存
                if ($pay_code == 'money') {
                    GoodsStoreCount($res);//减少库存增加销量
                    $msg = '支付成功';
                    $code = 200;
                    //扣除余额
                    if ($order_amount1 > 0) {
                        accountLog($this->user_id, -$order_amount1, '购物消费', 1, 0, $res, $pay['order_sn']);
                    }
                    //发放奖励
                    jl_base($res);
                } else {
                    if ($pay_code == 'mcbt') {
                        GoodsStoreCount($res);//减少库存增加销量
                        accountLog($this->user_id, -$order_amount1, '购物消费', 3, 0, $res, $pay['order_sn']);
                        $msg = '支付成功';
                        $code = 200;
                        //发放奖励
                        jl_base($res);
                    } else {
                        if (PlatformBaseService::isApp()) {
                            if ($pay_code == 'weixin') {
                                $wxpay = new WxPay($this->app);
                                $result = $wxpay->createWxData($pay['order_sn'], $order_amount1);
                                if ($result['code'] == 1) {
                                    if (getSysConfig('shopping.reduce') == 1) {
                                        GoodsStoreCount($res);//减少库存增加销量
                                    }
                                    $result_data['pay_code'] = 'wxpay';
                                    $result_data['result'] = $result['data'];
                                    throw new MyException("获取成功", 30, $result_data);
                                } else {
                                    throw new MyException("获取支付失败");
                                }
                            } else {
                                $alipay = new Alipay($this->app);
                                $result = $alipay->createData($pay['order_sn'], $order_amount1);
                                if (getSysConfig('shopping.reduce') == 1) {
                                    GoodsStoreCount($res);//减少库存增加销量
                                }
                                $result_data['pay_code'] = 'alipay';
                                $result_data['result'] = $result;
                                throw new MyException("获取成功", 30, $result_data);
                            }
                        } else {
                            $result = $platformService->createOrder($order);

                            throw new MyException("获取成功", 50, $result);
                        }
                    }
                }
                $return_data['res'] = $res;
                throw new MyException($msg, $code, $return_data);
            } else {
                throw new MyException("订单创建失败");
            }
        }
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['store_info'] = $store_info;
        throw new MyException("获取成功", 10, $data);
    }

    /**
     * @Apidoc\Title("订单列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/OrderList")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("status",type="int",default="all",desc="订单状态")
     */
    public function order_list()
    {
        $pi = input('pageIndex', 1);
        $ps = input('pageSize', 20);
        $status = input('status', 'all');
        $keyword = input('keyword');
        $order = new Order();
        $where[] = ['user_id', '=', $this->user_id];
        if ($status != 'all') {
            if ($status == 1) {
                $where[] = ['order_status', '=', -1];
            } else {
                $where[] = ['order_status', '=', $status - 1];
            }
        }
        if ($keyword) {
            $order_ids = Db::name('order_goods')
                ->whereLike('goods_name|item_name', "%$keyword%")
                ->group('order_id')
                ->column('order_id');
            $where[] = ['order_id', 'in', $order_ids];
        }
        $site_url = SITE_URL;
        $lists = $order->where($where)
            ->page($pi, $ps)
            ->order('order_id desc')
            ->field('order_id,order_sn,pay_code,total_amount,ship_price,order_amount,order_status,add_time')
            ->select()->each(function ($item) use ($site_url) {
                $order_goods = Db::name('order_goods')
                    ->where('order_id', $item['order_id'])
                    ->field("goods_name,concat('$site_url',goods_img) as goods_img,item_name,goods_num,goods_price")
                    ->select()->toArray();
                //计算商品总数
                $goods_all_num = Db::name('order_goods')->where('order_id', $item['order_id'])->sum('goods_num');
                $item['order_goods'] = $order_goods;
                $item['status_text'] = $item['order_status_text'];
                $item['goods_all_num'] = $goods_all_num;
                return $item;
            });
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['order'] = $lists;
        $data['store_info'] = $store_info;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("订单详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/OrderDetail")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     * @Apidoc\Param("is_zhifu",type="int",require=true,default="",desc="是否生成支付1是")
     * @Apidoc\Param("openid",type="string",default="",desc="openid")
     */
    public function order_detail(PlatFormFactory $platFormFactory)
    {
        $order_id = input('order_id/d', 0);
        $order = Order::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        $is_zhifu = input('is_zhifu');
        if ($is_zhifu == 1) {
            if ($order['order_status'] != -1) {
                throw new MyException("该订单已支付");
            }

            if (PlatformBaseService::isApp()) {
                if (!in_array($order['pay_code'], ['weixin', 'zfb'])) {
                    throw new MyException("订单信息有误");
                }

                if ($order['pay_code'] == 'weixin') {
                    $wxpay = new WxPay($this->app);
                    $result = $wxpay->createWxData($order['order_sn'], $order['order_amount']);
                    $return_data['pay_code'] = 'wxpay';
                    $return_data['result'] = $result['data'];
                    if ($result['code'] == 1) {
                        throw new MyException("获取成功", 50, $return_data);
                    } else {
                        throw new MyException("获取支付失败");
                    }
                } else {
                    $alipay = new Alipay($this->app);
                    $result = $alipay->createData($order['order_sn'], $order['order_amount']);
                    $return_data['pay_code'] = 'alipay';
                    $return_data['result'] = $result;
                    throw new MyException("获取成功", 50, $return_data);
                }
            } else {
                if (Carbon::now()->isAfter(Carbon::parse($order->add_time)->addMinutes(30))) {
                    throw new MyException('订单已失效，请重新下单', 400);
                }

                $platformObj = $platFormFactory->create();

                throw new MyException("获取成功", 60, $platformObj->createOrder($order));
            }
        }
        $order['goods'] = $order['order_goods'];
        $order['order_status_text'] = $order['order_status_text'];
        unset($order['order_goods']);
        if ($order['pay_time']) {
            $order['pay_time'] = date('Y-m-d H:i:s', $order['pay_time']);
        }
        if ($order['shipping_time']) {
            $order['shipping_time'] = date('Y-m-d H:i:s', $order['shipping_time']);
        }
        if ($order['confirm_time']) {
            $order['confirm_time'] = date('Y-m-d H:i:s', $order['confirm_time']);
        }
        if ($order['cancel_time']) {
            $order['cancel_time'] = date('Y-m-d H:i:s', $order['cancel_time']);
        }
        foreach ($order['goods'] as $value) {
            unset($value['pay_time'], $value['is_zhifu'], $value['order_id'], $value['jifen']);
            $value['goods_img'] = $value['goods_img'] ? SITE_URL . $value['goods_img'] : SITE_URL . '/static/images/not_adv.png';
        }
        unset($order['aid'], $order['reduce']);
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['order'] = $order;
        $data['store_info'] = $store_info;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("取消订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/CancelOrder")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function cancel_order()
    {
        $order_id = input('id/d', 0);
        $order = new Order();
        $order = $order->where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != -1) {
            throw new MyException('订单状态不允许取消');
        }
        if ($order['reduce'] == 1) {
            GoodsStoreCount($order_id, 2);//减少销量增加库存
        }
        $order_update['order_status'] = -2;
        $order_update['cancel_time'] = time();
        $row = $order->save($order_update);
        //        Db::name('order_goods')->where('order_id', $order_id)->delete();
        if ($row) {
            throw new MyException('操作成功', 200);
        } else {
            throw new MyException('操作失败');
        }
    }

    /**
     * @Apidoc\Title("物流信息")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/Logistics")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     */
    public function logistics()
    {
        $order_id = input('order_id/d', 0);
        $order = Order::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        $kuaidi = new KuaiDi100($this->app);
        $result = $kuaidi->query($order['shipping_code'], $order['shipping_number'], $order['mobile']);
        if ($result['code'] == 1) {
            $data['list'] = $result['result'];
            throw new MyException('操作成功', 200, $data);
        }
        throw new MyException($result['msg'], 200);
    }

    /**
     * @Apidoc\Title("确定收货")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/OrderConfirm")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function order_confirm()
    {
        $id = input('id/d', 0);
        $order = new Order();
        $order = $order->where(['user_id' => $this->user_id, 'order_id' => $id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != 2) {
            throw new MyException('该订单不能确认收货');
        }
        $res = Db::name('order')->where('order_id', $id)->update(['order_status' => 3, 'confirm_time' => time()]);
        if ($res) {
            //给商家结算 发放奖励
            store_settle($id);
            throw new MyException('收货成功', 200);
        } else {
            throw new MyException('收货失败');
        }
    }

    /**
     * @Apidoc\Title("创建认养订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/add_foster_order")
     * @Apidoc\Param("step",type="int",require=true,default="",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("pay_code",type="string",default="",desc="支付方式")
     * @Apidoc\Param("goods_id",type="int",default="",desc="商品id")
     * @Apidoc\Param("goods_num",type="int",default="",desc="购买商品数量")
     * @Apidoc\Param("user_note",type="string",default="",desc="用户备注")
     * @Apidoc\Param("paypwd",type="string",default="",desc="交易密码")
     */
    public function foster_confirm_order(PlatFormFactory $platFormFactory)
    {
        $step = input('step', 0);
        if (!in_array($step, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        //判断是否完善支付密码
        if (empty($this->user['paypwd'])) {
            throw new MyException('请先完善交易密码', -100);
        }
        $goods_id = input('goods_id');
        if (empty($goods_id)) {
            throw new MyException('提交参数有误', 100);
        }
        $orderModel = new FosterOrder();
        $goodsModel = new FosterGoods();
        $where = "is_on_sale=1 and goods_id={$goods_id}";
        $field = "store_id,goods_id,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_agreement,cost_price,day_sy,sy_day,yield,re_speed,goods_name,sales_sum,buy_max,unit,store_count,goods_remark,store_id,wid,extra_wid,extra_text";
        $goods = $goodsModel->with('store')->where($where)->field($field)->find();
        if (empty($goods)) {
            throw new MyException('商品不存在或已下架', 100);
        }
        $buy_max = $goods['buy_max'];
        //判断限购
        if ($buy_max > 0) {
            $count = $orderModel->where("goods_id={$goods_id} and user_id={$this->user_id}")->sum('goods_num');
            if ($count >= $buy_max) {
                $msg = sprintf('此商品限购%s件', $buy_max);
                throw new MyException($msg, 100);
            }
        }
        $platformService = $platFormFactory->create();
        $payList = $this->getMethods($platformService);

        //赠送储备金比例
        $cbj_bl = getSysConfig('rate.cbj_bl');
        if ($step == 1) {
            //商品数据
            $data['goods'] = $goods;
            $data['pay_list'] = $payList;
            throw new MyException('获取成功', 10, $data);
        } else {
            $goods_num = input('goods_num/d');
            $pay_code = input('pay_code');
            //用户备注
            $user_note = input('user_note');
            //交易密码
            $paypwd = input('paypwd');
            //            $openid = input('openid');
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 20);
            }
            $payMethod = $payList->where('code', $pay_code)->first();
            if (!$payMethod) {
                throw new MyException('支付方式有误', 20);
            }

            if ($goods_num > $goods['store_count']) {
                $msg = sprintf('此商品仅剩%s件', $goods['store_count']);
                throw new MyException($msg, 20);
            }
            //判断限购
            if ($buy_max > 0) {
                $count1 = $orderModel->where("goods_id={$goods_id} and user_id={$this->user_id}")->sum('goods_num');
                if ($count1 + $goods_num > $buy_max) {
                    $msg = sprintf('此商品限购%s件', $buy_max);
                    throw new MyException($msg, 20);
                }
            }
            $given_cbj = $goods['goods_price'] * $goods_num * $cbj_bl;
            $order_amount = $goods['goods_price'] * $goods_num;
            switch ($pay_code) {
                case 'money':
                    if ($this->user['user_money'] < $order_amount) {
                        $str = sprintf('粮票不足,可用%s!', $this->user['user_money']);
                        throw new MyException($str, 50);
                    }
                    if (empty($paypwd)) {
                        throw new MyException('请填写交易密码', 50);
                    }
                    if (encrypt($paypwd) != $this->user['paypwd']) {
                        throw new MyException('交易密码不正确', 50);
                    }
                    $order_insert['pay_name'] = '粮票';
                    $order_insert['pay_time'] = time();
                    $order_insert['order_status'] = 1;
                    break;
                case 'mcbt':
                    if ($this->user['mcbt'] < $order_amount) {
                        $str = sprintf('牧场补贴不足,可用%s!', $this->user['mcbt']);
                        throw new MyException($str, 50);
                    }
                    if (empty($paypwd)) {
                        throw new MyException('请填写交易密码', 50);
                    }
                    if (encrypt($paypwd) != $this->user['paypwd']) {
                        throw new MyException('交易密码不正确', 50);
                    }
                    $order_insert['pay_name'] = '牧场补贴';
                    $order_insert['pay_time'] = time();
                    $order_insert['order_status'] = 1;
                    break;
                default:
                    $order_insert['pay_name'] = $payMethod['name'];
                    break;
            }
            $reduce = getSysConfig('shopping.reduce');
            $order_insert['pay_code'] = $pay_code;
            $order_insert['reduce'] = $reduce;
            $order_insert['order_sn'] = getOrderSn('foster_order', 'f');//订单编号
            $order_insert['user_id'] = $this->user_id;//用户id
            $order_insert['goods_id'] = $goods_id;//商品id
            $order_insert['unit'] = $goods['unit'];//商品单位
            $order_insert['goods_name'] = $goods['goods_name'];//商品名称
            $order_insert['goods_img'] = str_replace($this->site_url, '', $goods['goods_img']);//商品主图
            $order_insert['goods_agreement'] = $goods['goods_agreement'];//商品协议
            $order_insert['goods_price'] = $goods['goods_price'];//商品价格
            $order_insert['cost_price'] = $goods['cost_price'];//成本价格
            $order_insert['goods_num'] = $goods_num;//商品数量
            $order_insert['total_amount'] = $order_amount;//订单总额
            $order_insert['order_amount'] = $order_amount;//订单总额
            $order_insert['wid'] = $goods['wid'];//收益钱包
            $order_insert['store_id'] = $goods['store_id'];//商家id
            $order_insert['day_sy'] = $goods['day_sy'];//每日收益
            $order_insert['sy_day'] = $goods['sy_day'];//收益周期
            $order_insert['yield'] = $goods['yield'] * $goods_num;//产量
            $order_insert['re_speed'] = $goods['re_speed'];//加速
            $order_insert['surplus_yield'] = $goods['yield'] * $goods_num;//剩余领取产量
            $order_insert['surplus_day'] = $goods['sy_day'];//剩余领取周期
            $order_insert['given_cbj'] = $given_cbj;//赠送储备金
            $order_insert['extra_wid'] = $goods['extra_wid'];//额外wallet表id
            $order_insert['extra_text'] = $goods['extra_text'];//额外奖励信息
            $order_insert['user_note'] = $user_note;
            $orderModel->save($order_insert);
            $res = $orderModel->order_id;
            if ($res) {
                //如果余额支付直接扣除库存
                if ($pay_code == 'money') {
                    FosterGoodsStoreCount($res);//减少库存增加销量
                    $msg = '支付成功';
                    $code = 200;
                    //扣除余额
                    accountLog($this->user_id, -$order_amount, '购物消费', 1, 0, $res, $order_insert['order_sn']);
                    //发放奖励
                    foster_jl_base($res);
                } else {
                    if ($pay_code == 'mcbt') {
                        FosterGoodsStoreCount($res);//减少库存增加销量
                        accountLog($this->user_id, -$order_amount, '购物消费', 3, 0, $res, $order_insert['order_sn']);
                        $msg = '支付成功';
                        $code = 200;
                        //发放奖励
                        foster_jl_base($res);
                    } else {
                        if (PlatformBaseService::isApp()) {
                            if ($pay_code == 'weixin') {
                                $wxpay = new WxPay($this->app);
                                $result = $wxpay->createWxData($order_insert['order_sn'], $order_amount);
                                if ($result['code'] == 1) {
                                    if ($reduce == 1) {
                                        FosterGoodsStoreCount($res);//减少库存增加销量
                                    }
                                    $data['pay_code'] = 'wxpay';
                                    $data['result'] = $result['data'];
                                    throw new MyException("获取成功", 30, $data);
                                } else {
                                    throw new MyException("获取支付失败");
                                }
                            } else {
                                $alipay = new Alipay($this->app);
                                $result = $alipay->createData($order_insert['order_sn'], $order_amount);
                                if ($reduce == 1) {
                                    FosterGoodsStoreCount($res);//减少库存增加销量
                                }
                                $data['pay_code'] = 'alipay';
                                $data['result'] = $result;
                                throw new MyException("获取成功", 30, $data);
                            }
                        } else {
                            $result = $platformService->createOrder($orderModel);

                            throw new MyException('', 50, $result);
                        }
                    }
                }
                $return_data['res'] = $res;
                throw new MyException($msg, $code, $return_data);
            } else {
                throw new MyException("订单创建失败");
            }
        }
    }

    /**
     * @Apidoc\Title("取消认养订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ry_dd_cancel")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function cancel_foster_order()
    {
        $order_id = input('id/d', 0);
        $order = new FosterOrder();
        $order = $order->where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != -1) {
            throw new MyException('订单状态不允许取消');
        }
        if ($order['reduce'] == 1) {
            FosterGoodsStoreCount($order_id, 2);//减少销量增加库存
        }
        $order_update['order_status'] = -2;
        $order_update['cancel_time'] = time();
        $row = $order->save($order_update);
        if ($row) {
            throw new MyException('操作成功', 200);
        } else {
            throw new MyException('操作失败');
        }
    }

    /**
     * @Apidoc\Title("认养订单列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/get_ry_dd_list")
     * @Apidoc\Param (ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("status",type="int",default="all",desc="订单状态")
     */
    public function foster_order_list()
    {
        $pi = input('pageIndex', 1);
        $ps = input('pageSize', 20);
        $status = input('status');
        $order = new FosterOrder();
        $where[] = ['user_id', '=', $this->user_id];
        if ($status) {
            $where[] = ['order_status', '=', $status];
        }
        $today = strtotime(date("Y-m-d"));
        $field = "order_id,user_id,order_sn,order_status,feed_status,unit,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_num,
        day_sy,speed,sy_day,yield,yet_sy,surplus_yield,surplus_day,pay_code,total_amount,ship_price,order_amount,add_time,pay_time,store_id";
        $list = $order->where($where)
            ->with(['store'])
            ->page($pi, $ps)
            ->order('speed desc,order_id desc')
            ->field($field)
            ->select()->each(function ($item) use ($today) {
                $item['status_text'] = $item['order_status_text'];
                $item['buy_mobile'] = $item['user']['mobile'];
                if ($item['pay_time'] > $today) {
                    $item['can_feed'] = 0;
                } else {
                    $item['can_feed'] = 1;
                }
                unset($item['user']);
                return $item;
            });
        $data['list'] = $list;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("认养订单详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ry_dd_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="1",desc="订单id")
     * @Apidoc\Param("is_zhifu",type="int",require=true,default="",desc="是否生成支付1是")
     */
    public function foster_order_detail(PlatFormFactory $platFormFactory)
    {
        $order_id = input('order_id/d', 0);
        $order = FosterOrder::with(['store'])->where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        $is_zhifu = input('is_zhifu');
        if ($is_zhifu == 1) {
            if ($order['order_status'] != -1) {
                throw new MyException("该订单已支付");
            }

            if (PlatformBaseService::isApp()) {
                if (!in_array($order['pay_code'], ['weixin', 'zfb'])) {
                    throw new MyException("订单信息有误");
                }
                if ($order['pay_code'] == 'weixin') {
                    $wxpay = new WxPay($this->app);
                    $result = $wxpay->createWxData($order['order_sn'], $order['order_amount']);
                    $data['pay_code'] = 'wxpay';
                    $data['result'] = $result['data'];
                    if ($result['code'] == 1) {
                        throw new MyException("获取成功", 50, $data);
                    } else {
                        throw new MyException("获取支付失败");
                    }
                } else {
                    $alipay = new Alipay($this->app);
                    $result = $alipay->createData($order['order_sn'], $order['order_amount']);
                    $data['pay_code'] = 'alipay';
                    $data['result'] = $result;
                    throw new MyException("获取成功", 50, $data);
                }
            } else {
                if (Carbon::now()->isAfter(Carbon::parse($order->add_time)->addMinutes(30))) {
                    throw new MyException('订单已失效，请重新下单', 400);
                }

                $platformObj = $platFormFactory->create();

                throw new MyException("获取成功", 60, $platformObj->createOrder($order));
            }
        }
        $order['ry_time'] = date('Y年m月d日 H:i:s', $order->getData('add_time'));
        $order['surplus_day_text'] = $order['surplus_day_text'];
        $order['status_text'] = $order['order_status_text'];
        if ($order['pay_time']) {
            $order['pay_time'] = date('Y-m-d h:i:s', $order['pay_time']);
        }
        $order['goods_img'] = SITE_URL . $order['goods_img'];
        $data['order'] = $order;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("认养订单喂食/领取收益")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ry_dd_feed")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     * @Apidoc\Param("type",type="int",require=true,default="",desc="类型1喂食2领取收益")
     */
    public function foster_order_feed()
    {
        $type = input('type');
        if (!in_array($type, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        $order_id = input('order_id/d', 0);
        $order = FosterOrder::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        if ($type == 1) {
            //判断是否已完成签到
            if ($this->user['is_fodder'] == 0) {
                $msg = sprintf('饲料不足');
                throw new MyException($msg);
            }
            $today = strtotime(date("Y-m-d"));
            //判断是否还有收益可领取 是否是今日的订单
            if ($order['surplus_yield'] <= 0 || $order['order_status'] < 0 || $order['pay_time'] > $today || $order['feed_status'] != 1) {
                throw new MyException('此订单无需喂食');
            }
            //更新订单
            $order_update['feed_status'] = 2;
            $log = $order->save($order_update);
            if ($log) {
                throw new MyException('操作成功', 200);
            } else {
                throw new MyException('操作失败');
            }
        } else {
            //判断是否已喂食
            if ($order['surplus_yield'] <= 0 || $order['order_status'] != 1 || $order['feed_status'] != 2) {
                throw new MyException('此订单无收益领取');
            }
            //发放奖励
            $day_sy = min(($order['day_sy'] * $order['goods_num']) + $order['speed'], $order['surplus_yield']);
            $log = $log1 = true;
            if ($day_sy > 0) {
                $log = user_wallet_log($this->user_id, 0, $day_sy, '每日收益', 0, $order['wid']);
                //更新订单
                $order_update['feed_status'] = 3;
                $order_update['yet_sy'] = Db::Raw("yet_sy+{$day_sy}");
                /********************{此处修改}********************/
                $day_sy1 = round($day_sy / 10, 3);
                $order_update['surplus_yield'] = Db::Raw("surplus_yield-{$day_sy1}");
                $order_update['surplus_day'] = Db::Raw("surplus_day-1");
                if ($order['surplus_yield'] - $day_sy <= 0) {
                    $order_update['order_status'] = 2;
                    $order_update['end_time'] = time();
                }
                $log1 = $order->save($order_update);
            }
            if ($log && $log1) {
                throw new MyException('操作成功', 200);
            } else {
                throw new MyException('操作失败');
            }
        }
    }

    /**
     * @Apidoc\Title("创建兑换订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/add_dh_order")
     * @Apidoc\Param("step",type="int",require=true,default="",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("pay_code",type="string",default="",desc="支付方式")
     * @Apidoc\Param("goods_id",type="int",default="",desc="商品id")
     * @Apidoc\Param("goods_num",type="int",default="",desc="购买商品数量")
     * @Apidoc\Param("delivery",type="int",default="",desc="配送方式")
     * @Apidoc\Param("user_note",type="string",default="",desc="用户备注")
     * @Apidoc\Param("paypwd",type="string",default="",desc="交易密码")
     */
    public function convert_confirm_order()
    {
        $step = input('step', 0);
        if (!in_array($step, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        //判断是否完善支付密码
        if (empty($this->user['paypwd'])) {
            throw new MyException('请先完善交易密码', -100);
        }
        $address_id = input('address_id');
        $goods_id = input('goods_id');
        $goods_num = input("goods_num/d");// 商品数量
        if ($goods_num <= 0 || !is_numeric($goods_num)) {
            throw new MyException('购买数量有误', 100);
        }
        if (empty($goods_id)) {
            throw new MyException('提交参数有误', 100);
        }
        if ($address_id) {
            $add = Db::name('user_address')->where('address_id', $address_id)->find();
            $temp_address_id = $address_id;
        } else {
            $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
            $temp_address_id = $add['address_id'];
        }
        $orderModel = new ConvertOrder();
        $goodsModel = new ConvertGoods();
        $where = "is_on_sale=1 and goods_id={$goods_id}";
        $field = "goods_id,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_weight,goods_remark,wid,store_count";
        $goods = $goodsModel->where($where)->field($field)->find();
        if (empty($goods)) {
            throw new MyException('商品不存在或已下架', 100);
        }
        if ($goods['store_count'] < $goods_num) {
            throw new MyException('商品库存不足', 100);
        }
        $wallet = $goods['qianbao'];
        $name = $wallet['name'];
        $user_money = UserWallet::where("user_id=$this->user_id and wid={$goods['wid']}")->value('money');
        $user_money = $user_money ?: 0;
        $pay_list = [
            [
                'code' => 'money',
                'name' => "{$name}支付({$user_money})",
            ]
        ];
        $arr = [];
        foreach ($pay_list as $pay) {
            $arr[] = $pay['code'];
        }
        //赠送储备金比例
        $ship_price = $goods->get_ship_price($goods_id, $temp_address_id, $goods_num);
        unset($goods['qianbao']);
        if ($step == 1) {
            $store_info['store_title'] = getSysConfig('basic.store_title');
            $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
            //商品数据
            $data['goods'] = $goods;
            $data['add'] = $add;
            $data['store_info'] = $store_info;
            $data['pay_list'] = $pay_list;
            $data['ship_price'] = $ship_price;
            $data['name'] = $name;
            throw new MyException('获取成功', 10, $data);
        } else {
            $pay_code = input('pay_code');
            //用户备注
            $user_note = input('user_note');
            $delivery = input('delivery');
            //交易密码
            $paypwd = input('paypwd');
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 50);
            }
            if (!in_array($pay_code, $arr)) {
                throw new MyException('支付方式有误', 50);
            }
            if (!in_array($delivery, [1, 2])) {
                throw new MyException('配送方式有误', 50);
            }
            if (empty($add)) {
                throw new MyException('请选择收货地址', 50);
            }
            if ($goods_num > $goods['store_count']) {
                $msg = sprintf('此商品仅剩%s件', $goods['store_count']);
                throw new MyException($msg, 50);
            }
            $ship_price = $delivery == 1 ? $ship_price : 0;
            $order_amount = $goods['goods_price'] * $goods_num + $ship_price;
            $user_money = UserWallet::where("user_id=$this->user_id and wid={$goods['wid']}")->value('money');
            $user_money = $user_money ?: 0;
            if ($user_money < $order_amount) {
                $str = sprintf('%s不足,可用%s!', $name, $user_money);
                throw new MyException($str, 50);
            }
            if (empty($paypwd)) {
                throw new MyException('请填写交易密码', 50);
            }
            if (encrypt($paypwd) != $this->user['paypwd']) {
                throw new MyException('交易密码不正确', 50);
            }
            //扣除相应资金
            $log = user_wallet_log($this->user_id, 0, -$order_amount, '兑换商品', 0, $goods['wid']);
            if ($log) {
                $order_insert['order_sn'] = getOrderSn('convert_order', 'c');//订单编号
                $order_insert['user_id'] = $this->user_id;//用户id
                $order_insert['order_status'] = 1;//订单状态
                $order_insert['goods_id'] = $goods_id;//商品id
                $order_insert['goods_name'] = $goods['goods_name'];//商品名称
                $order_insert['goods_img'] = str_replace($this->site_url, '', $goods['goods_img']);//商品主图
                $order_insert['goods_price'] = $goods['goods_price'];//商品价格
                $order_insert['goods_num'] = $goods_num;//商品数量
                $order_insert['wid'] = $goods['wid'];//wallet表id
                $order_insert['delivery'] = $delivery;//配送方式
                $order_insert['total_amount'] = $goods['goods_price'] * $goods_num;//订单总额
                $order_insert['ship_price'] = $ship_price;//运费
                $order_insert['order_amount'] = $order_amount;//订单总额
                $order_insert['address_id'] = $address_id;//user_address表id
                $order_insert['consignee'] = $add['consignee'];//收货人
                $order_insert['mobile'] = $add['mobile'];//手机
                $order_insert['province'] = $add['province'];//省份
                $order_insert['city'] = $add['city'];//城市
                $order_insert['district'] = $add['district'];//县区
                $order_insert['address'] = $add['address'];//详细地址
                $order_insert['user_note'] = $user_note;
                $order_insert['pay_code'] = $pay_code;
                $order_insert['pay_name'] = $name;
                $order_insert['pay_time'] = time();
                $orderModel->save($order_insert);
                $res = $orderModel->order_id;
                if ($res) {
                    //扣除商品库存 增加销量
                    $goods_update['store_count'] = Db::Raw("store_count-{$goods_num}");
                    $goods_update['sales_sum'] = Db::Raw("sales_sum+{$goods_num}");
                    $goods->save($goods_update);
                    throw new MyException("支付成功", 200);
                } else {
                    throw new MyException("订单创建失败");
                }
            } else {
                throw new MyException("订单创建失败");
            }
        }
    }

    /**
     * @Apidoc\Title("兑换订单列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/get_dh_dd_list")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("status",type="int",default="all",desc="订单状态")
     */
    public function convert_order_list()
    {
        $pi = input('pageIndex', 1);
        $ps = input('pageSize', 20);
        $status = input('status', '0');
        $order = new ConvertOrder();
        $where[] = ['user_id', '=', $this->user_id];
        if ($status != '0') {
            $where[] = ['order_status', '=', $status];
        }
        $field = "order_id,user_id,order_sn,order_status,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_num,
        delivery,pay_code,pay_name,total_amount,ship_price,order_amount,add_time";
        $list = $order->where($where)
            ->page($pi, $ps)
            ->order('order_id desc')
            ->field($field)
            ->select()->each(function ($item) {
                $item['status_text'] = $item['order_status_text'];
                $item['buy_mobile'] = $item['user']['mobile'];
                $item['time_text'] = date('Y-m-d', $item->getData('add_time'));
                unset($item['user']);
                return $item;
            });
        $data['list'] = $list;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("兑换订单详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/dh_dd_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     */
    public function convert_order_detail()
    {
        $order_id = input('order_id/d', 0);
        $order = ConvertOrder::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        $order['order_status_text'] = $order['order_status_text'];
        $order['delivery_text'] = $order['delivery_text'];
        unset($order['order_goods']);
        if ($order['pay_time']) {
            $order['pay_time'] = date('Y-m-d h:i:s', $order['pay_time']);
        }
        if ($order['shipping_time']) {
            $order['shipping_time'] = date('Y-m-d h:i:s', $order['shipping_time']);
        }
        if ($order['hx_time']) {
            $order['hx_time'] = date('Y-m-d h:i:s', $order['hx_time']);
        }
        if ($order['confirm_time']) {
            $order['confirm_time'] = date('Y-m-d h:i:s', $order['confirm_time']);
        }
        $order['goods_img'] = $this->site_url . $order['goods_img'];
        //生成自提码
        if ($order['delivery'] == 2) {
            require_once root_path('vendor') . "phpqrcode/phpqrcode.php";
            $text = "cityRanch_{$order_id}";
            $img_name = md5("zhang{$order_id}zhang");
            \QRcode::png($text, 'upload/order/pick_up/' . $img_name . '.png', 1, 6, 1);
            $img_url = SITE_URL . "/upload/order/pick_up/{$img_name}.png";
            $order['zt_ewm'] = $img_url;
        }
        unset($order['address_id'], $order['transaction_id']);
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['order'] = $order;
        $data['store_info'] = $store_info;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("兑换订单核销")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/dh_dd_hx")
     * @Apidoc\Param("step",type="int",require=true,default="",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     */
    public function convert_order_verification()
    {
        $step = input('step');
        if (empty($step) || !in_array($step, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        $order_id = input('order_id/d', 0);
        if (empty($order_id)) {
            throw new MyException('提交参数有误', 100);
        }
        $order = ConvertOrder::where(['order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        //判断当前会员是否是自提点
        if (empty($this->user['pick']) || $this->user['pick']['status'] != 1) {
            throw new MyException('无权限核销', 100);
        }
        //判断订单是否已核销
        if ($order['order_status'] != 1) {
            throw new MyException('该订单已核销', 100);
        }
        if ($step == 1) {
            $order['order_status_text'] = $order['order_status_text'];
            $order['delivery_text'] = $order['delivery_text'];
            unset($order['order_goods']);
            if ($order['pay_time']) {
                $order['pay_time'] = date('Y-m-d h:i:s', $order['pay_time']);
            }
            if ($order['shipping_time']) {
                $order['shipping_time'] = date('Y-m-d h:i:s', $order['shipping_time']);
            }
            if ($order['confirm_time']) {
                $order['confirm_time'] = date('Y-m-d h:i:s', $order['confirm_time']);
            }
            $order['goods_img'] = $this->site_url . $order['goods_img'];
            unset($order['address_id'], $order['transaction_id']);
            $store_info['store_title'] = getSysConfig('basic.store_title');
            $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
            $data['order'] = $order;
            $data['store_info'] = $store_info;
            throw new MyException('获取成功', 10, $data);
        } else {
            $order_update['pick_id'] = $this->user['pick']['id'];
            $order_update['hx_time'] = time();
            $order_update['order_status'] = 2;
            $res = $order->save($order_update);
            if ($res) {
                throw new MyException('核销成功', 100);
            } else {
                throw new MyException('核销失败');
            }
        }
    }

    /**
     * @Apidoc\Title("兑换订单收货")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/dh_dd_confirm")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function convert_order_confirm()
    {
        $id = input('id/d', 0);
        $order_model = new ConvertOrder();
        $order = $order_model->where(['user_id' => $this->user_id, 'order_id' => $id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != 2) {
            throw new MyException('该订单不能确认收货');
        }
        $order->order_status = 3;
        $order->confirm_time = time();
        $res = $order->save();
        if ($res) {
            //如果是自提的 自提点获得相对应的某票，外加订单金额百分之多少的粮票补贴
            if ($order['delivery'] == 2 && $order['pick']) {
                $wallet = $order['qianbao'];
                $pick = $order['pick'];
                $order_amount = $order['order_amount'];
                if ($order_amount > 0) {
                    user_wallet_log($pick['user_id'], 0, $order_amount, "核销赠送", $this->user_id, $order['wid']);
                    if ($wallet['bt_bl'] > 0) {
                        /********************{此处修改 补贴需*交易价格}********************/
                        $order_amount = $order_amount * $wallet['jy_price'];
                        $ew_amount = $order_amount * ($wallet['bt_bl'] / 100);
                        $ew_amount = round($ew_amount, 2);
                        if ($ew_amount > 0) {
                            accountLog($pick['user_id'], $ew_amount, '核销补贴', 1, $this->user_id, 0, '', $ew_amount);
                        }
                    }
                }
            }
            throw new MyException('收货成功', 200);
        } else {
            throw new MyException('收货失败');
        }
    }

    /**
     * @Apidoc\Title("创建积分订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/add_jifen_order")
     * @Apidoc\Param("step",type="int",require=true,default="",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("pay_code",type="string",default="",desc="支付方式")
     * @Apidoc\Param("goods_id",type="int",default="",desc="商品id")
     * @Apidoc\Param("goods_num",type="int",default="",desc="购买商品数量")
     * @Apidoc\Param("user_note",type="string",default="",desc="用户备注")
     * @Apidoc\Param("paypwd",type="string",default="",desc="交易密码")
     */
    public function jifen_confirm_order()
    {
        $step = input('step', 0);
        if (!in_array($step, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        //判断是否完善支付密码
        if (empty($this->user['paypwd'])) {
            throw new MyException('请先完善交易密码', -100);
        }
        $address_id = input('address_id');
        $goods_id = input('goods_id');
        $goods_num = input("goods_num/d");// 商品数量
        if ($goods_num <= 0 || !is_numeric($goods_num)) {
            throw new MyException('购买数量有误', 100);
        }
        if (empty($goods_id)) {
            throw new MyException('提交参数有误', 100);
        }
        if ($address_id) {
            $add = Db::name('user_address')->where('address_id', $address_id)->find();
        } else {
            $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
        }
        $orderModel = new JifenOrder();
        $goodsModel = new JifenGoods();
        $where = "is_on_sale=1 and goods_id={$goods_id}";
        $field = "goods_id,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_remark,store_count";
        $goods = $goodsModel->where($where)->field($field)->find();
        if (empty($goods)) {
            throw new MyException('商品不存在或已下架', 100);
        }
        if ($goods['store_count'] < $goods_num) {
            throw new MyException('商品库存不足', 100);
        }
        $pay_list = [
            [
                'code' => 'money',
                'name' => "积分支付({$this->user['jifen']})",
            ]
        ];
        $arr = [];
        foreach ($pay_list as $pay) {
            $arr[] = $pay['code'];
        }
        if ($step == 1) {
            $store_info['store_title'] = getSysConfig('basic.store_title');
            $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
            //商品数据
            $data['goods'] = $goods;
            $data['add'] = $add;
            $data['store_info'] = $store_info;
            $data['pay_list'] = $pay_list;
            throw new MyException('获取成功', 10, $data);
        } else {
            $pay_code = input('pay_code');
            //用户备注
            $user_note = input('user_note');
            //交易密码
            $paypwd = input('paypwd');
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 50);
            }
            if (!in_array($pay_code, $arr)) {
                throw new MyException('支付方式有误', 50);
            }
            if (empty($add)) {
                throw new MyException('请选择收货地址', 50);
            }
            if ($goods_num > $goods['store_count']) {
                $msg = sprintf('此商品仅剩%s件', $goods['store_count']);
                throw new MyException($msg, 50);
            }
            $order_amount = $goods['goods_price'] * $goods_num;
            if ($this->user['jifen'] < $order_amount) {
                $str = sprintf('积分不足,可用%s!', $this->user['jifen']);
                throw new MyException($str, 50);
            }
            if (empty($paypwd)) {
                throw new MyException('请填写交易密码', 50);
            }
            if (encrypt($paypwd) != $this->user['paypwd']) {
                throw new MyException('交易密码不正确', 50);
            }
            //扣除相应资金
            $log = accountLog($this->user_id, -$order_amount, '购物消费', 2);
            if ($log) {
                $order_insert['order_sn'] = getOrderSn('jifen_order', 'j');//订单编号
                $order_insert['user_id'] = $this->user_id;//用户id
                $order_insert['order_status'] = 1;//订单状态
                $order_insert['goods_id'] = $goods_id;//商品id
                $order_insert['goods_name'] = $goods['goods_name'];//商品名称
                $order_insert['goods_img'] = str_replace($this->site_url, '', $goods['goods_img']);//商品主图
                $order_insert['goods_price'] = $goods['goods_price'];//商品价格
                $order_insert['goods_num'] = $goods_num;//商品数量
                $order_insert['total_amount'] = $goods['goods_price'] * $goods_num;//订单总额
                $order_insert['ship_price'] = 0;//运费
                $order_insert['order_amount'] = $order_amount;//订单总额
                $order_insert['address_id'] = $address_id;//user_address表id
                $order_insert['consignee'] = $add['consignee'];//收货人
                $order_insert['mobile'] = $add['mobile'];//手机
                $order_insert['province'] = $add['province'];//省份
                $order_insert['city'] = $add['city'];//城市
                $order_insert['district'] = $add['district'];//县区
                $order_insert['address'] = $add['address'];//详细地址
                $order_insert['user_note'] = $user_note;
                $order_insert['pay_code'] = $pay_code;
                $order_insert['pay_name'] = '积分支付';
                $order_insert['pay_time'] = time();
                $orderModel->save($order_insert);
                $res = $orderModel->order_id;
                if ($res) {
                    //扣除商品库存 增加销量
                    $goods_update['store_count'] = Db::Raw("store_count-{$goods_num}");
                    $goods_update['sales_sum'] = Db::Raw("sales_sum+{$goods_num}");
                    $goods->save($goods_update);
                    throw new MyException("支付成功", 200);
                } else {
                    throw new MyException("订单创建失败");
                }
            } else {
                throw new MyException("订单创建失败");
            }
        }
    }

    /**
     * @Apidoc\Title("积分订单列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/get_jf_dd_list")
     * @Apidoc\Param (ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("status",type="int",default="all",desc="订单状态")
     */
    public function jifen_order_list()
    {
        $pi = input('pageIndex', 1);
        $ps = input('pageSize', 20);
        $status = input('status', '0');
        $order = new JifenOrder();
        $where[] = ['user_id', '=', $this->user_id];
        if ($status != '0') {
            $where[] = ['order_status', '=', $status];
        }
        $field = "order_id,user_id,order_sn,order_status,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_num,
        pay_code,pay_name,total_amount,ship_price,order_amount,add_time";
        $list = $order->where($where)
            ->page($pi, $ps)
            ->order('order_id desc')
            ->field($field)
            ->select()->each(function ($item) {
                $item['status_text'] = $item['order_status_text'];
                $item['buy_mobile'] = $item['user']['mobile'];
                $item['time_text'] = date('Y-m-d', $item->getData('add_time'));
                unset($item['user']);
                return $item;
            });
        $data['list'] = $list;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("积分订单详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/jf_dd_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     */
    public function jifen_order_detail()
    {
        $order_id = input('order_id/d', 0);
        $order = JifenOrder::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        $order['order_status_text'] = $order['order_status_text'];
        unset($order['order_goods']);
        if ($order['pay_time']) {
            $order['pay_time'] = date('Y-m-d h:i:s', $order['pay_time']);
        }
        if ($order['shipping_time']) {
            $order['shipping_time'] = date('Y-m-d h:i:s', $order['shipping_time']);
        }
        if ($order['confirm_time']) {
            $order['confirm_time'] = date('Y-m-d h:i:s', $order['confirm_time']);
        }
        $order['goods_img'] = $this->site_url . $order['goods_img'];
        unset($order['address_id'], $order['transaction_id']);
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['order'] = $order;
        $data['store_info'] = $store_info;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("积分订单收货")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/jf_dd_confirm")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function jifen_order_confirm()
    {
        $id = input('id/d', 0);
        $order_model = new JifenOrder();
        $order = $order_model->where(['user_id' => $this->user_id, 'order_id' => $id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != 2) {
            throw new MyException('该订单不能确认收货');
        }
        $order->order_status = 3;
        $order->confirm_time = time();
        $res = $order->save();
        if ($res) {
            throw new MyException('收货成功', 200);
        } else {
            throw new MyException('收货失败');
        }
    }

    /**
     * @Apidoc\Title("创建进货订单")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/add_jh_order")
     * @Apidoc\Param("step",type="int",require=true,default="",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("pay_code",type="string",default="",desc="支付方式")
     * @Apidoc\Param("goods_id",type="int",default="",desc="商品id")
     * @Apidoc\Param("goods_num",type="int",default="",desc="购买商品数量")
     * @Apidoc\Param("user_note",type="string",default="",desc="用户备注")
     * @Apidoc\Param("paypwd",type="string",default="",desc="交易密码")
     */
    public function stock_confirm_order()
    {
        $step = input('step', 0);
        if (!in_array($step, [1, 2])) {
            throw new MyException('提交参数有误', 100);
        }
        //判断是否开通了自提点
        if (empty($this->user['pick']) || $this->user['pick']['status'] != 1) {
            throw new MyException('尚未开通自提点', 100);
        }
        //判断是否完善支付密码
        if (empty($this->user['paypwd'])) {
            throw new MyException('请先完善交易密码', -100);
        }
        $address_id = input('address_id');
        $goods_id = input('goods_id');
        $goods_num = input("goods_num/d");// 商品数量
        if ($goods_num <= 0 || !is_numeric($goods_num)) {
            throw new MyException('购买数量有误', 100);
        }
        if (empty($goods_id)) {
            throw new MyException('提交参数有误', 100);
        }
        if ($address_id) {
            $add = Db::name('user_address')->where('address_id', $address_id)->find();
        } else {
            $add = Db::name('user_address')->where('user_id=' . $this->user_id . ' and is_default=1')->find();
        }
        $orderModel = new StockOrder();
        $goodsModel = new StockGoods();
        $where = "is_on_sale=1 and goods_id={$goods_id}";
        $field = "goods_id,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_remark,wid,store_count";
        $goods = $goodsModel->where($where)->field($field)->find();
        if (empty($goods)) {
            throw new MyException('商品不存在或已下架', 100);
        }
        if ($goods['store_count'] < $goods_num) {
            throw new MyException('商品库存不足', 100);
        }
        $wallet = $goods['qianbao'];
        $name = $wallet['name'];
        $user_money = UserWallet::where("user_id=$this->user_id and wid={$goods['wid']}")->value('money');
        $user_money = $user_money ?: 0;
        $pay_list = [
            [
                'code' => 'money',
                'name' => "{$name}支付({$user_money})",
            ]
        ];
        $arr = [];
        foreach ($pay_list as $pay) {
            $arr[] = $pay['code'];
        }
        unset($goods['qianbao']);
        if ($step == 1) {
            $store_info['store_title'] = getSysConfig('basic.store_title');
            $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
            //商品数据
            $data['goods'] = $goods;
            $data['add'] = $add;
            $data['store_info'] = $store_info;
            $data['pay_list'] = $pay_list;
            $data['name'] = $name;
            throw new MyException('获取成功', 10, $data);
        } else {
            $pay_code = input('pay_code');
            //用户备注
            $user_note = input('user_note');
            //交易密码
            $paypwd = input('paypwd');
            if ($goods_num <= 0 || !is_numeric($goods_num)) {
                throw new MyException('购买数量有误', 50);
            }
            if (!in_array($pay_code, $arr)) {
                throw new MyException('支付方式有误', 50);
            }
            if (empty($add)) {
                throw new MyException('请选择收货地址', 50);
            }
            if ($goods_num > $goods['store_count']) {
                $msg = sprintf('此商品仅剩%s件', $goods['store_count']);
                throw new MyException($msg, 50);
            }
            $order_amount = $goods['goods_price'] * $goods_num;
            $user_money = UserWallet::where("user_id=$this->user_id and wid={$goods['wid']}")->value('money');
            $user_money = $user_money ?: 0;
            if ($user_money < $order_amount) {
                $str = sprintf('%s不足,可用%s!', $name, $user_money);
                throw new MyException($str, 50);
            }
            if (empty($paypwd)) {
                throw new MyException('请填写交易密码', 50);
            }
            if (encrypt($paypwd) != $this->user['paypwd']) {
                throw new MyException('交易密码不正确', 50);
            }
            //扣除相应资金
            $log = user_wallet_log($this->user_id, 0, -$order_amount, '兑换商品', 0, $goods['wid']);
            if ($log) {
                $order_insert['order_sn'] = getOrderSn('stock_order', 's');//订单编号
                $order_insert['user_id'] = $this->user_id;//用户id
                $order_insert['order_status'] = 1;//订单状态
                $order_insert['goods_id'] = $goods_id;//商品id
                $order_insert['goods_name'] = $goods['goods_name'];//商品名称
                $order_insert['goods_img'] = str_replace($this->site_url, '', $goods['goods_img']);//商品主图
                $order_insert['goods_price'] = $goods['goods_price'];//商品价格
                $order_insert['goods_num'] = $goods_num;//商品数量
                $order_insert['total_amount'] = $goods['goods_price'] * $goods_num;//订单总额
                $order_insert['ship_price'] = 0;//运费
                $order_insert['order_amount'] = $order_amount;//订单总额
                $order_insert['address_id'] = $address_id;//user_address表id
                $order_insert['consignee'] = $add['consignee'];//收货人
                $order_insert['mobile'] = $add['mobile'];//手机
                $order_insert['province'] = $add['province'];//省份
                $order_insert['city'] = $add['city'];//城市
                $order_insert['district'] = $add['district'];//县区
                $order_insert['address'] = $add['address'];//详细地址
                $order_insert['user_note'] = $user_note;
                $order_insert['pay_code'] = $pay_code;
                $order_insert['pay_name'] = $name;
                $order_insert['pay_time'] = time();
                $orderModel->save($order_insert);
                $res = $orderModel->order_id;
                if ($res) {
                    //扣除商品库存 增加销量
                    $goods_update['store_count'] = Db::Raw("store_count-{$goods_num}");
                    $goods_update['sales_sum'] = Db::Raw("sales_sum+{$goods_num}");
                    $goods->save($goods_update);
                    throw new MyException("支付成功", 200);
                } else {
                    throw new MyException("订单创建失败");
                }
            } else {
                throw new MyException("订单创建失败");
            }
        }
    }

    /**
     * @Apidoc\Title("进货订单列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/get_jh_dd_list")
     * @Apidoc\Param (ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("status",type="int",default="all",desc="订单状态")
     */
    public function stock_order_list()
    {
        $pi = input('pageIndex', 1);
        $ps = input('pageSize', 20);
        $status = input('status', '0');
        $order = new StockOrder();
        $where[] = ['user_id', '=', $this->user_id];
        if ($status != '0') {
            $where[] = ['order_status', '=', $status];
        }
        $field = "order_id,user_id,order_sn,order_status,goods_name,concat('$this->site_url',goods_img) as goods_img,goods_price,goods_num,
        pay_code,pay_name,total_amount,ship_price,order_amount,add_time";
        $list = $order->where($where)
            ->page($pi, $ps)
            ->order('order_id desc')
            ->field($field)
            ->select()->each(function ($item) {
                $item['status_text'] = $item['order_status_text'];
                $item['buy_mobile'] = $item['user']['mobile'];
                $item['time_text'] = date('Y-m-d', $item->getData('add_time'));
                unset($item['user']);
                return $item;
            });
        $data['list'] = $list;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("进货订单详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/jh_dd_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("order_id",type="int",require=true,default="",desc="订单id")
     */
    public function stock_order_detail()
    {
        $order_id = input('order_id/d', 0);
        $order = StockOrder::where(['user_id' => $this->user_id, 'order_id' => $order_id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在', 100);
        }
        $order['order_status_text'] = $order['order_status_text'];
        unset($order['order_goods']);
        if ($order['pay_time']) {
            $order['pay_time'] = date('Y-m-d h:i:s', $order['pay_time']);
        }
        if ($order['shipping_time']) {
            $order['shipping_time'] = date('Y-m-d h:i:s', $order['shipping_time']);
        }
        if ($order['confirm_time']) {
            $order['confirm_time'] = date('Y-m-d h:i:s', $order['confirm_time']);
        }
        $order['goods_img'] = $this->site_url . $order['goods_img'];
        unset($order['address_id'], $order['transaction_id']);
        $store_info['store_title'] = getSysConfig('basic.store_title');
        $store_info['wap_login_logo'] = SITE_URL . getSysConfig('basic.wap_login_logo');
        $data['order'] = $order;
        $data['store_info'] = $store_info;
        throw new MyException('获取成功', 200, $data);
    }

    /**
     * @Apidoc\Title("进货订单收货")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/jh_dd_confirm")
     * @Apidoc\Param("user_id",type="int",require=true,default="1",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="1",desc="订单id")
     */
    public function stock_order_confirm()
    {
        $id = input('id/d', 0);
        $order_model = new StockOrder();
        $order = $order_model->where(['user_id' => $this->user_id, 'order_id' => $id])->find();
        if (empty($order)) {
            throw new MyException('订单不存在');
        }
        if ($order['order_status'] != 2) {
            throw new MyException('该订单不能确认收货');
        }
        $order->order_status = 3;
        $order->confirm_time = time();
        $res = $order->save();
        if ($res) {
            throw new MyException('收货成功', 200);
        } else {
            throw new MyException('收货失败');
        }
    }

    /**
     * @param \app\common\service\PlatformBaseService $platformBaseService
     * @return \Illuminate\Support\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getMethods(PlatformBaseService $platformBaseService): Collection
    {
        $platformPayMethod = $platformBaseService->getPayMethods();

        $pluginPayments = Plugin::where('scene', 0)->where('status', 1)->select();


        foreach ($pluginPayments as &$payment) {
            if ($payment['code'] === 'money') {
                $payment['name'] = '粮票支付(' . $this->user['user_money'] . ')';
            } elseif ($payment['code'] === 'mcbt') {
                $payment['name'] = '牧场补贴支付(' . $this->user['mcbt'] . ')';
            }

        }

        return new Collection(array_merge($platformPayMethod->toArray(), $pluginPayments->toArray()));
    }
}
